import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import { Message } from 'element-ui';
import {constants} from "@/common/constants";


Vue.use(VueRouter)
let currentUser=(localStorage.getItem("user")!=null)?JSON.parse(localStorage.getItem("user")):{};

const routes = [
  {
    path: '/',
    redirect: '/login',
  },
  {
    path: '/login',
    name: 'login',
    component:()=>import('@/views/BackendSystem/Login.vue'),
  },
  //  后台布局
  {
    path: '/backendFramework/',
    name: 'BackendFramework',
    component:()=>import('@/views/BackendSystem/BackendFramework.vue'),
    beforeEnter: (to, from, next) => {
      if (constants.role.admin==currentUser.role) {
        next(); // 允许访问
      } else {
        Message.error("权限不足!");
        next('/login'); // 重定向到登录页面
      }
    },
    children:[
      {
        path: 'blog',
        name: 'Blog',
        component:()=>import('@/views/BackendSystem/Blog.vue'),
      },{
        path: 'comment',
        name: 'Comment',
        component:()=>import('@/views/BackendSystem/Comment.vue'),
      },{
        path: 'inform',
        name: 'Inform',
        component:()=>import('@/views/BackendSystem/Inform.vue'),
      },{
        path: 'advertisement',
        name: 'Advertisement',
        component:()=>import('@/views/BackendSystem/Advertisement.vue'),
      },{
        path: 'label',
        name: 'Label',
        component:()=>import('@/views/BackendSystem/Label.vue'),
      },{
        path: 'profile',
        name: 'Profile',
        component:()=>import('@/views/BackendSystem/Profile.vue'),
      },{
        path: 'system/dataSetting',
        name: 'Setting',
        component:()=>import('@/views/BackendSystem/DataSetting.vue'),
      }
    ]
  },
//    前台布局
  {
    path: '/frontFramework/',
    name: 'FrontFramework',
    component:()=>import('@/views/FrontSystem/FrontFramework.vue'),

    children:[
      {
        path: '', // 当访问 /frontFramework/ 时，默认跳转到这个子路由
        redirect: 'frontIndex' // 'subRoutePath' 是你想要默认跳转到的子路由路径
      },
      {
        path: 'submitFilmSource',
        name: 'SubmitFilmSource',
        component:()=>import('@/views/FrontSystem/SubmitFilmSource.vue'),
      },
      {
        path: 'frontIndex',
        name: 'FrontIndex',
        component:()=>import('@/views/FrontSystem/FrontIndex.vue'),
      },
      {
        path: 'blogManage',
        name: 'BlogManage',
        component:()=>import('@/views/FrontSystem/BlogManage.vue'),
      },
      {
        path: 'informManage',
        name: 'InformManage',
        component:()=>import('@/views/FrontSystem/InformManage.vue'),
      },
      {
        path: 'blogView',
        name: 'BlogView',
        component:()=>import('@/views/FrontSystem/BlogView.vue'),
      },
      {
        path: 'history',
        name: 'HistoryManage',
        component:()=>import('@/views/FrontSystem/HistoryManage.vue'),
      },
      {
        path: 'profile',
        name: 'Profile',
        component:()=>import('@/views/BackendSystem/Profile.vue'),
      },
      {
        path: 'messageInform',
        name: 'MessageInform',
        component:()=>import('@/views/FrontSystem/MessageInform.vue'),
      }

    ]
  },

]

const router = new VueRouter({
  mode:"history",
  routes: routes,
})

//路由守卫，在所有url之前执行
router.beforeEach((to,from,next)=>{
  //判断是否在blogView界面，是否显示评论按钮
  if (to.path.includes("blogView")){
    store.commit('checkBlogView',{
      blogId: to.query.id,
    })
  }else{
    store.commit('checkBlogView',{
      blogId: null,
    })
  }
  next();
})

export default router
